package com.sikao.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.CategoryAttrForInviteBean;
import com.sikao.app.ui.AddInviteTwoActivity;
import com.sikao.app.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 发布招标第二步 类型是4 多选的子项
 */
public class AddInviteStepTwoTypeFourChildItemAdapter extends BaseAdapter {

    CategoryAttrForInviteBean.ResBean resBean;//父级
    private List<CategoryAttrForInviteBean.ResBean.AttrValuesBean> objects = new ArrayList<CategoryAttrForInviteBean.ResBean.AttrValuesBean>();
    List<CategoryAttrForInviteBean.ResBean.DefaultValueBean> defaultValueBeanList=new ArrayList<>();//默认值
    private Context context;
    private LayoutInflater layoutInflater;
    int index=0;

    public AddInviteStepTwoTypeFourChildItemAdapter(Context context, CategoryAttrForInviteBean.ResBean resBean, int index) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.resBean=resBean;
        objects=resBean.getAttr_values();
        defaultValueBeanList= resBean.getDefault_value();
        this.index=index;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CategoryAttrForInviteBean.ResBean.AttrValuesBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.mll_add_step_prices_type_four_child_item, null);
            convertView.setTag(new ViewHolder(convertView));
       // }
        doSelected(position, convertView, parent);
        initializeViews(getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }
    private void doSelected(int position, View convertView, ViewGroup parent) {
        //默认值
       // Utils.showLog("====avb进入默认值"+ AddInviteTwoActivity.initFlagTypeFour);
        if(!AddInviteTwoActivity.initFlagTypeFour) {

            CategoryAttrForInviteBean.ResBean.AttrValuesBean object = getItem(position);
            ViewHolder holder = (ViewHolder) convertView.getTag();
            for (int i = 0; i < AddInviteTwoActivity.resTypeFour.get(index).getDefault_value().size(); i++) {
                if (object.getId() == AddInviteTwoActivity.resTypeFour.get(index).getDefault_value().get(i).getId()) {
                    holder.tvFourChildValue.setTextColor(Color.RED);//
                    holder.tvFourChildValue.setBackgroundResource(R.drawable.shap_textview_border_red);
                    //修改多选数据组 属性值
                    AddInviteTwoActivity.resTypeFour.get(index).getAttr_values().get(position).setChecked(true);

                }

                if (parent != null) {
                    int size_ = parent.getChildCount();
                    //检查当前列表的个数，与数据的个数是否相等，即ListView已经初步加载完成
                    if (size_ > 0 && objects.size() == size_) {//至此，可以判定该处理接口已经成功执行过一次
                        Utils.showLog("====avb完成默认值 子总数量"+size_);
                        AddInviteTwoActivity.initFlagTypeFour = true;
                    }
                }
            }
        }
    }
    private void initializeViews(CategoryAttrForInviteBean.ResBean.AttrValuesBean object,  final ViewHolder holder, final int position) {
        //TODO implement
        holder.tvFourChildValue.setText(object.getValue());
        if(AddInviteTwoActivity.canEditForInvite){
            holder.tvFourChildValue.setEnabled(true);
            holder.tvFourChildValue.setClickable(true);

        }else{
            holder.tvFourChildValue.setEnabled(false);
            holder.tvFourChildValue.setClickable(false);

        }
        holder.tvFourChildValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AddInviteTwoActivity.resTypeFour.get(index).getAttr_values().get(position).isChecked()){//如果本身已经点击过则恢复原样
                    AddInviteTwoActivity.resTypeFour.get(index).getAttr_values().get(position).setChecked(false);
                    holder.tvFourChildValue.setTextColor(Color.BLACK);//恢复默认颜色
                    holder.tvFourChildValue.setBackgroundResource(R.drawable.shap_textview_border_gray);
                }else{
                    AddInviteTwoActivity.resTypeFour.get(index).getAttr_values().get(position).setChecked(true);
                    holder.tvFourChildValue.setTextColor(Color.RED);//
                    holder.tvFourChildValue.setBackgroundResource(R.drawable.shap_textview_border_red);
                }
            }
        });
    }

    protected class ViewHolder {
        private TextView tvFourChildValue;

        public ViewHolder(View view) {
            tvFourChildValue = (TextView) view.findViewById(R.id.tv_four_child_value);
        }
    }
}
